package domain.presentation.chartview;

import java.util.List;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

import domain.presentationmodel.metricview.MetricViewPresentationModel;

public class MetricLineChartView extends ViewPart {

	public static final String ID = "domain.presentation.chartview.MetricLineChartView"; //$NON-NLS-1$
	private FormData formData_1;
	private MetricViewPresentationModel metricViewPresentationModel = MetricViewPresentationModel.getMetricViewPresentationModel();
	private Table table;
	private FormData formData_2;

	public MetricLineChartView() {
	}
	
	class MetricNameLabelProvider extends LabelProvider{
		public String getText(Object obj) {
			if(obj instanceof String){
				return (String)obj;
			}
			return null;
		}	
	}
	
	class MetricNameContentProvider {
		
	}
	
	private static CategoryDataset createDataSet(){
		// row keys...
		String series1 = "First";
		String series2 = "Second";

		// column keys...
		String category1 = "Label 1";
		String category2 = "Label 2";
		String category3 = "Label  3";
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();


		dataset.addValue(1.0, series1, category1);
		dataset.addValue(4.0, series1, category2);
		dataset.addValue(3.0, series1, category3);

		dataset.addValue(5.0, series2, category1);
		dataset.addValue(7.0, series2, category2);
		dataset.addValue(6.0, series2, category3);

		return dataset;

	}
	
	private JFreeChart createChart(CategoryDataset dataset){
		// create the chart...
        JFreeChart chart = ChartFactory.createLineChart(
            "Line Chart Demo 1",       // chart title
            "Type",                    // domain axis label
            "Value",                   // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            true,                      // include legend
            true,                      // tooltips
            false                      // urls
        );
		return chart;
	}


	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new FormLayout());
		Composite containerChart = new Composite(parent, SWT.NONE);
		{
			formData_1 = new FormData();
			formData_1.left = new FormAttachment(0, 10);
			formData_1.top = new FormAttachment(0, 10);
			formData_1.bottom = new FormAttachment(100, -10);
			formData_1.width = 412;
			containerChart.setLayoutData(formData_1);
		}
		
		containerChart.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			Group grpMetricsList = new Group(parent, SWT.NONE);
			formData_1.right = new FormAttachment(100, -197);
			grpMetricsList.setToolTipText("Select the metrics to be considered in the graph.");
			grpMetricsList.setLayout(new FormLayout());
			grpMetricsList.setText("Metrics List");
			{
				FormData formData = new FormData();
				formData.left = new FormAttachment(containerChart, 6);
				formData.right = new FormAttachment(100, -10);
				formData.width = 144;
				formData.top = new FormAttachment(0, 10);
				formData.bottom = new FormAttachment(100, -10);
				formData.height = 442;
				grpMetricsList.setLayoutData(formData);
			}
			{
				CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(grpMetricsList, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
				table = checkboxTableViewer.getTable();
				
				//checkboxTableViewer.setContentProvider(new MetricNameContentProvider());
				checkboxTableViewer.setLabelProvider(new MetricNameLabelProvider());
				
				createRows(table);
				
				
				{
					formData_2 = new FormData();
					formData_2.top = new FormAttachment(0, 10);
					formData_2.right = new FormAttachment(100, -10);
					formData_2.height = 227;
					formData_2.left = new FormAttachment(0, 10);
					table.setLayoutData(formData_2);
				}
			}
			{
				Button btnCleanSelectedMetrics = new Button(grpMetricsList, SWT.CHECK);
				formData_2.bottom = new FormAttachment(100, -32);
				{
					FormData formData = new FormData();
					formData.top = new FormAttachment(table, 6);
					formData.left = new FormAttachment(table, 0, SWT.LEFT);
					formData.bottom = new FormAttachment(100, -10);
					btnCleanSelectedMetrics.setLayoutData(formData);
				}
				btnCleanSelectedMetrics.setText("clear selection(s)");
			}
		}
		
		/*JFreeChart chart = createChart(createDataSet());
		final ChartComposite frame = new ChartComposite(containerChart, SWT.NONE,
				chart, true);*/

		createActions();
		initializeToolBar();
		initializeMenu();
	}	

	private void createRows(Table table) {
		List<String> metricNameList = metricViewPresentationModel.getMetricList();
		for (String metricName : metricNameList) {
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(metricName);
		}
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
	
	public void openLineChartView() {
		try {
			@SuppressWarnings("unused")
			IViewPart viewPart = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getActivePage().showView(ID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
		
	}
}
